﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using XMK.Tadong.Biz;
using Ext.Net;
using XMK.Tadong.Web.Language;
namespace XMK.Tadong.Web.Farpoint
{
    public partial class Default : FileExplorerPage
    {
        //private static string userId;  //用户id
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Session["UserID"] = Request.Cookies["_userId_"].Value;// "48f3889c-af8d-401f-ada2-c383031af92d1";//暂时指定固定值 之后需要屏蔽掉
            
            this.Session["YY"] = "cn";  //暂时固定值
            string languageStr = Request.Cookies["_language_"].Value;
            if (languageStr == "0")
            {
                this.Session["YY"] = "en";
            }
            
            if (this.Session["UserID"] != null)
            {
                //userId = this.Session["UserID"].ToString();
            }
            if (!Page.IsPostBack)
            {
                this.TreePanel1.DataBind();
                this.Panel5.DataBind();
                this.Image1.DataBind();
                initializeTables();
                //插入日志
                string IP = Request.ServerVariables["REMOTE_ADDR"].Equals("::1") ? "127.0.0.1" : Request.ServerVariables["REMOTE_ADDR"];
                XMK.Tadong.Model.Cond.Base_PageLog pageLogModel = new XMK.Tadong.Model.Cond.Base_PageLog();
                pageLogModel.pageName = "Structured_data_entry";
                pageLogModel.ip = IP;
                XMKLogBiz.WriteLogScanPage(pageLogModel);
            }
        }

        /// <summary>
        /// 获得结构化数据表名对应关系
        /// </summary>
        private void initializeTables()
        {

            DataTable dt = new DataTable();

            XMK.Tadong.StructuredData_IntoDb.Data.DBTree db = new XMK.Tadong.StructuredData_IntoDb.Data.DBTree(Config.TadongDbProvider);

            try
            {
                dt = db.GetInputTree();
                if (dt != null)
                {
                    InitTree(dt);
                }
                else
                {
                    XMK.Tadong.StructuredData_IntoDb.Data.JsAlert.Alert(lang.Get("Database read error"));
                }
            }
            catch
            {
                XMK.Tadong.StructuredData_IntoDb.Data.JsAlert.Alert(lang.Get("Database read error"));
            }
            finally
            {
                dt.Dispose();
            }

        }
        /// <summary>
        /// 初始化树数据
        /// </summary>
        /// <param name="dt">数据表</param>
        /// <param name="tvw">treeview</param>
        private void InitTree(DataTable dt)
        {
            TreePanel1.Items.Clear();
            Node root = new Node();
            bool isEN = true;
            if (this.Session["YY"] != null && this.Session["YY"].ToString() == "cn")
            {
                isEN = false;
            }
            System.Data.DataView dv = new System.Data.DataView(dt);
            foreach (DataRowView drv in dv)
            {
                Node treeNode1 = new Node();
                treeNode1.NodeID = drv["TABLE_CODE"].ToString();
                treeNode1.Text = drv["TABLE_CODE"].ToString();
                if (isEN)
                {
                    treeNode1.Qtip = drv["TABLE_ENNAME"].ToString();
                    treeNode1.Text = drv["TABLE_CODE"].ToString();
                }
                else
                {
                    treeNode1.Qtip = drv["TABLE_NAME"].ToString();
                    treeNode1.Text = drv["TABLE_NAME"].ToString();
                }

                treeNode1.Href = "BasicData.aspx?table_code=" + Server.UrlEncode(treeNode1.NodeID) + "&AUTOCOLUMN=ID&table_name=" + Server.UrlEncode(treeNode1.Text);
                treeNode1.Leaf = true;
                root.Children.Add(treeNode1);
            }
            TreePanel1.Root.Add(root);
        }
    }
}